package ApplicationsYoung;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;



public class ApplicationsYoung extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
    public ApplicationsYoung() {
        super();
       
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("*****보육신청 서블릿*****");
		
		request.setCharacterEncoding("UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		response.setContentType("text/html; charset=UTF-8");
 		RequestDispatcher view = null;
 		MultipartRequest multi = null;
 		
 		ApplicationsYoungDAO youngdao = new ApplicationsYoungDAO();
 		ApplicationsYoungBean youngbean = new ApplicationsYoungBean();

 		PrintWriter out =response.getWriter();
 		String action=request.getParameter("action");
 		String id=null;
 		if(request.getParameter("id") != null){
 			id=request.getParameter("id");
 		}
 		
 		if(action.equals("read")){
 			
 			int idnumber = youngdao.idGet(id);
 			int inquiry = youngdao.babyPlusInquiry(idnumber);
 			if(inquiry == 1 || inquiry == 3 || inquiry == 5){
 				String pagename = "/Applications/Young/Fm_MassageView";
 				request.setAttribute("inquiry", inquiry);
 				request.setAttribute("page", pagename);
 				view=request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
 				view.forward(request,response);
 			}else if(inquiry == 0){
 				String pagename = "/Applications/Young/Fm_AppYoung";
 				request.setAttribute("page", pagename);
 				view=request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
 				view.forward(request,response);
 			}

 		}else if(action.equals("delet")){
 			
 				int idnumber = youngdao.idGet(id);
 				youngdao.cancelSet(idnumber);

 System.out.println("id넘버 = " + idnumber);
				out.println( "<script>" );
				out.println( "location.href=\"/KOMORIUTA/MYoungAction.do?\";" );
				out.println( "</script>" );
 			
 		}else if(action.equals("back")){
 			
 				out.println( "<script>" );
				out.println( "location.href=\"/KOMORIUTA/MYoungAction.do?\";" );
				out.println( "</script>" );

 		}else if(action.equals("add")){
 		
 				
 			String saveFolder = getServletContext().getRealPath("Applications/Young/photo");
 			String encType = "UTF-8";
 			int maxSize = 1*1024*1024;
 			String filename = "";
 			
 			multi = new MultipartRequest( request, saveFolder, maxSize, encType,
 					new DefaultFileRenamePolicy() );
 			
 			id=multi.getParameter("id");
 			
 			int idnumber = youngdao.idGet(id);
 			int gender = Integer.parseInt(multi.getParameter("remember"));
 			String kind = multi.getParameter("remember1");
 			String textarea = "内容無";
 			if(multi.getParameter("textarea") != ""){
 				textarea = multi.getParameter("textarea");
 			}
 			youngbean.setMembers_Id(idnumber);
 			youngbean.setBby_Add(youngdao.membersAddressGet(idnumber));
 			youngbean.setBby_Kind(kind);
 			youngbean.setBby_Gender(gender);
 			youngbean.setBby_Countents(textarea);
 			youngbean.setBby_Photo(multi.getOriginalFileName("parent"));
 			youngbean.setBby_Money(multi.getParameter("parttime"));
 			ArrayList<String> certificatelist ;
 			
 			int certificatesum = 0;
 			int daysum = 0;
 			
 			String[] certificate = null;
 			if(multi.getParameter("certificate") != null){
 				certificate = multi.getParameterValues("certificate");
 				certificatelist = new ArrayList<String>(Arrays.asList(certificate));
 				if(multi.getParameter("and")!=null && !"".equals(multi.getParameter("and"))){

					certificatelist.add(multi.getParameter("and"));
 				}
				certificate= Arrays.copyOf(certificatelist.toArray(), certificatelist.size(), String[].class);

				certificatesum = youngdao.abliltySet(certificate, idnumber);
 			}else if(multi.getParameter("and") != null){
 				youngdao.abliltySet(multi.getParameter("and"), idnumber);
 				certificatesum = 64;
 			}
 			
 			if(multi.getParameter("certificate_Day_All") != null){
 				String dayall = multi.getParameter("certificate_Day_All");
 				youngdao.daySet(dayall, idnumber);
 				daysum = 128;
 			}else{
 				String[] day = multi.getParameterValues("certificate_Day");
 				daysum = youngdao.daySet2(day, idnumber);
 			}
 			youngdao.adminYoungSet(id, idnumber);
			youngdao.babyPlusSet(youngbean);
			youngdao.abliltySumSet(idnumber, certificatesum);
			youngdao.daySumSet(idnumber, daysum);
			
			int bbpid = youngdao.babyPlusIdGet(idnumber);
 					
 			String[] name1 = multi.getParameterValues("name_H");
 			String[] name2 = multi.getParameterValues("name_K");
 			String[] birthday = multi.getParameterValues("birthday");		
 			String[] num = multi.getParameterValues("num");
 	
 			for(int i=0;i<num.length;i++){
 				
 				String childgender = multi.getParameter("child_gender"+num[i]);
 				String file = multi.getOriginalFileName("file"+num[i]);
 				
 				youngbean.setBabyplus_Id(bbpid);
 				youngbean.setBbp_NameH(name1[i]);
 				youngbean.setBbp_NameK(name2[i]);
 				youngbean.setBbp_Birth(Date.valueOf(birthday[i]));
 				youngbean.setBbp_Gender(Integer.parseInt(childgender));
 				youngbean.setBbp_Photo(file);
 				
				youngdao.babyPhotoSet(youngbean);
 							
System.out.println("--------------------------");	
System.out.println(i+" 번째 아이 정보 -");
System.out.println(" 한자 이름 : " + name1[i]);
System.out.println(" 히라가나 이름 : " + name2[i]);
System.out.println(" 생일 : " + birthday[i]);
System.out.println(" 성별 : " + childgender);
System.out.println(" 사진 파일 명 : " + file);
System.out.println("--------------------------");

 				
 			}
 			
			out.println( "<script>" );
			out.println( "location.href=\"/KOMORIUTA/MYoungAction.do?\";" );
			out.println( "</script>" );

 		}
		
	}

}
